﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Customize.Excel.Import
{
    /// <summary>
    /// Excel导入到Model的基类
    /// </summary>
    public abstract class ExcelEntity : IDataErrorInfo
    {

        private readonly Dictionary<string, PropertyInfo> _propertyGetters = new Dictionary<string, PropertyInfo>();
        private readonly Dictionary<string, ValidationAttribute[]> _validators = new Dictionary<string, ValidationAttribute[]>();
        private readonly Type _type;

        public ExcelEntity()
        {
            _type = GetType();
            LoadData();
        }
        #region 私有方法
        protected virtual void LoadData()
        {
            PropertyInfo[] properties = _type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo propertyInfo in properties)
            {
                //拥有验证特性
                object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(ValidationAttribute), true);
                if (customAttributes.Length > 0)
                {
                    _validators.Add(propertyInfo.Name, customAttributes as ValidationAttribute[]);
                    _propertyGetters.Add(propertyInfo.Name, propertyInfo);
                }
            }
        }
        #endregion

        /// <summary>  
        /// WPF使用不到该属性  
        /// </summary>  
        public virtual string Error
        {
            get
            {
                IEnumerable<string> errors = from d in _validators
                                             from v in d.Value
                                             where !v.IsValid(_propertyGetters[d.Key].GetValue(this, null))
                                             select v.ErrorMessage;
                #region 验证属性
                //var context = new ValidationContext(this, null, null);
                //var results = new List<ValidationResult>();
                //System.ComponentModel.DataAnnotations.Validator.TryValidateObject(this, context, results, true);
                //IEnumerable<string> errors = from d in results
                //                             select d.ErrorMessage;
                #endregion 
                return string.Join(System.Environment.NewLine, errors.ToArray());
            }
        }

        /// <summary>  
        /// 验证  
        /// </summary>  
        /// <param name="columnName"></param>  
        /// <returns></returns>  
        public virtual string this[string columnName]
        {
            get
            {
                if (_propertyGetters.ContainsKey(columnName))
                {
                    object value = _propertyGetters[columnName].GetValue(this, null);
                    string[] errors = _validators[columnName].Where(v => !v.IsValid(value))
                        .Select(v => v.ErrorMessage).ToArray();

                    return string.Join(System.Environment.NewLine, errors);
                }
                return string.Empty;

                //var vc = new ValidationContext(this, null, null);
                //vc.MemberName = columnName;
                //var res = new List<ValidationResult>();
                //var result = System.ComponentModel.DataAnnotations.Validator.TryValidateProperty(this.GetType().GetProperty(columnName).GetValue(this, null), vc, res);
                //if (res.Count > 0)
                //{
                //    return string.Join(Environment.NewLine, res.Select(r => r.ErrorMessage).ToArray());
                //}
            }
        }

    }
}
